/*
*	Copyright (C) 2014  Julio Montes, Mario Carrillo
*	
*	Julio Montes e-mail: imc.coder@gmail.com 
*
*	Mario Carrillo e-mail: mario_90ago@live.com.mx
*
*	BSD 3-Clause License
*/

#include "sdk/include/lexermgr.h"
#include "sdk/include/lexer.h"

#include "sdk/include/path.h"

#include <wx/dir.h>

namespace sdk
{
    template<> LexerManager *sdk::Mgr<LexerManager>::instance = 0;

    LexerManager::LexerManager()
    {
        wxArrayString l_files;
        wxDir::GetAllFiles( sdk::Path::s_dirLexers, &l_files, wxT( "*.lexer" ), wxDIR_FILES );

        for( size_t i=0; i<l_files.Count(); ++i )
        {
            const wxString &l_currentFile = l_files[i];
            if( l_currentFile.EndsWith( wxT( ".lexer" ) ) )
            {
                sdk::Lexer *l_lexer = new sdk::Lexer;

                if( l_lexer->LoadFile( l_currentFile ) )
                    m_vectorLexers.push_back( l_lexer );
                else
                    delete l_lexer;
            }
        }
    }

    LexerManager::~LexerManager()
    {

    }

    sdk::Lexer *LexerManager::GetLexerByFileMask( const wxString &fileMask ) const
    {
        if( fileMask.IsEmpty() )
            return NULL;

        for( size_t i=0; i<m_vectorLexers.size(); ++i )
        {
            if( m_vectorLexers[i]->HasFileMask( fileMask ) )
                return m_vectorLexers[i];
        }

        return NULL;
    }

    sdk::Lexer *LexerManager::GetLexerBySupportedFile( const wxString &filename ) const
    {
        for( size_t i=0; i<m_vectorLexers.size(); ++i )
        {
            if( m_vectorLexers[i]->HasSupportedFile( filename ) )
                return m_vectorLexers[i];
        }

        return NULL;
    }

    sdk::Lexer *LexerManager::GetLexerByName( const wxString &lexer ) const
    {
        for( size_t i=0; i<m_vectorLexers.size(); ++i )
        {
            if( m_vectorLexers[i]->GetName() == lexer )
                return m_vectorLexers[i];
        }

        return NULL;
    }
}
